package com.dz.file.controller;

import com.dz.common.base.BaseApiService;
import com.dz.common.base.ResponseBase;
import com.dz.file.design.FileContext;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @ClassName : FileController
 * @Description :
 * @Author : ming
 * @Date: 2020-12-01 10:30
 **/
@RestController
@RequestMapping("/file")
public class FileController extends BaseApiService {

    @PostMapping(value = "upload")
    @ApiOperation(value = "文件上传")
    public ResponseBase upload(@RequestParam(name = "file") MultipartFile file) throws Exception {
        if(file!=null) {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
            FileContext fileContext = new FileContext(suffix);
            String url = fileContext.upload(fileName, file.getInputStream());
            return setResultSuccess(url);
        }
        return setResultError("失败");
    }
}
